//package com.bonc.extractor;
//
//import java.io.File;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.Set;
//
//import org.apache.commons.io.FileUtils;
//
//import com.bonc.classifier.maxent.MaxentTrainer;
//import com.bonc.vectorspacemodel.Corpus;
//import com.bonc.vectorspacemodel.Document;
//import com.bonc.vectorspacemodel.VectorSpaceModel;
//
//import cc.mallet.classify.Classifier;
//import cc.mallet.classify.ClassifierTrainer;
//import cc.mallet.classify.MaxEntTrainer;
//import cc.mallet.classify.Trial;
//import cc.mallet.types.Alphabet;
//import cc.mallet.types.FeatureVector;
//import cc.mallet.types.Instance;
//import cc.mallet.types.InstanceList;
//import cc.mallet.types.LabelAlphabet;
//import cc.mallet.util.Randoms;
//
///**
// * @author donggui@bonc.com.cn
// * @version 2016 2016年6月22日 下午2:26:29
// */
//public class MaxentChiTester {
//	
//    public static List<String> getFileList(String sPath)  {
////    	List<String> matchedFiles = new ArrayList<String>();
//    	List<String> matchedFileNames = new ArrayList<String>();
//    	
//    	//如果sPath不以文件分隔符结尾，自动添加文件分隔符
//        if (!sPath.endsWith(File.separator)) {
//            sPath = sPath + File.separator;
//        }
//        
//        File dirFile = new File(sPath);
//		//如果dir对应的文件不存在，或者不是一个目录，则退出
//		if (!dirFile.exists() || !dirFile.isDirectory()) {
//		    return null;
//		} else {
//			File[] files = dirFile.listFiles();
//			for (int i = 0; i < files.length; i++) {
//			    //判断文件是需要的文件
//			    if (files[i].isFile()) {
//			    	String fileName = files[i].getName();
//			    	matchedFileNames.add(fileName);
////			    	matchedFiles.add(files[i].getAbsolutePath());
//			    } 
//			}
//		}		
//        
//		return matchedFileNames;
//    }
//
//	public static void main(String[] args) throws IOException {
//
//		Map<String, String> stopwords = VectorAndClusterChiTester.loadStopwords();
//		
//		String sPath = "C:\\Users\\Administrator\\git\\hierarchical-clustering-java\\data\\events\\";
//		List<String> fileNames = getFileList(sPath);
//		
//		ArrayList<Document> documents = new ArrayList<Document>();
//        ArrayList<String> targetValue = new ArrayList<String>();
//        
//		for(String fname:fileNames){
//			int k = 1;
//			File file = new File(sPath+fname);
//			String content = FileUtils.readFileToString(file);
//			String[] events = content.split("\n");
//			for(String event: events){
//				String id = fname + k;
//				System.out.println(id+"=="+event);
//				Document document = new Document(id,event,true,stopwords);
//				documents.add(document);
//				targetValue.add(fname.replaceAll(".txt", ""));
//				k++;
//			}
//		}		
//		
//		Corpus corpus = new Corpus(documents);
//		VectorSpaceModel vectorSpace = new VectorSpaceModel(corpus);		
//		int row = documents.size();
//		Set<String> terms = corpus.getInvertedIndex().keySet();		
//
//		//maxent
//        Alphabet featureAlphabet = new Alphabet();//特征词典
//        LabelAlphabet targetAlphabet = new LabelAlphabet();//类标词典
//        targetAlphabet.lookupIndex("pos");
//        targetAlphabet.lookupIndex("neg");
//        targetAlphabet.stopGrowth();
////      featureAlphabet.lookupIndex("f1");
////      featureAlphabet.lookupIndex("f2");
//        for (String term : terms) {
//        	featureAlphabet.lookupIndex(term);	       	
//        }
//
//        InstanceList allInstances = new InstanceList (featureAlphabet,targetAlphabet);//实例集对象
//        
//        int featuresize = terms.size();
//        int i = 0;
//		for (Document document : corpus.getDocuments()) {
//			System.out.print("document "+(document.getFileName()+"===="));
//			HashMap<String, Double> weights = vectorSpace.getTfIdfWeights().get(document);
//			double[] featureValues1 = new double[featuresize];
//			int j = 0;
//			for (String term : terms) {				
//				Double weight = weights.get(term);
//				if(weight !=null ){
//					featureValues1[j] = weight.doubleValue();
//				}else{
//					featureValues1[j] = 0.0;
//				}
////				System.out.print(featureValues1[j]);
//				j++;				
//			}
//			System.out.println();
//			
//			
////            FeatureVector featureVector = new FeatureVector(featureAlphabet,
////                    (String[])targetAlphabet.toArray(new String[size]),featureValue);//change list to array
//            
//            FeatureVector featureVector = new FeatureVector(featureAlphabet,terms.toArray(new String[featuresize]),featureValues1);//change list to array
//            
//            Instance instance = new Instance (featureVector,targetAlphabet.lookupLabel(targetValue.get(i)), document.getFileName(),null);
//            
//            i++;
//            allInstances.add(instance);
//		}
//		
//		double Gaussian_Variance = 1.0;
//		ClassifierTrainer trainer = new MaxEntTrainer(Gaussian_Variance);
//		
//		InstanceList trainingInstances = allInstances.subList(0, row-2);
//		Instance testinstance = allInstances.get(row-1);
//		Classifier maxentclassifier = trainer.train(trainingInstances);
//
//        MaxentTrainer myTrainer = new MaxentTrainer();
//        
//        System.out.println(myTrainer.predict(maxentclassifier, testinstance));
//        System.out.println(testinstance.getName());
//        
//        //test2
//        int TRAINING = 0;
//        int TESTING = 1;
//        int VALIDATION = 2;
//    
//        // Split the input list into training (90%) and testing (10%) lists.
//        InstanceList[] instanceLists = allInstances.split(new Randoms(), new double[] {0.9, 0.1, 0.0});
////        Classifier classifier = trainClassifier(instanceLists[TRAINING]);
//        Classifier maxentclassifier2 = trainer.train(instanceLists[TRAINING]);
//        Trial trial = new Trial(maxentclassifier2, instanceLists[TESTING]);
//        System.out.println("Accuracy: " + trial.getAccuracy());                                                      
//        System.out.println("F1 for class 'pos': " + trial.getF1("pos"));
//        System.out.println("Precision for class '" +
//        		maxentclassifier2.getLabelAlphabet().lookupLabel("pos") + "': " +
//                           trial.getPrecision("pos"));
//	}
//}
